<?php
$conn = mysql_connect('localhost','root','') or die("Error to connect to
server" . mysql_error());
mysql_select_db('testweb',$conn) or die("Error to select the database" . mysql_error());


require_once 'C:\sphinx\sphinxapi.php';



$search_result = '';
function getRecords($id, $dblink) {
       $sql = "SELECT products_name,products_description,short_desc,products_url FROM `products_description` WHERE products_id =
        '{$id}'";  
        $rs = mysql_query($sql, $dblink) or die(mysql_error());
        $row = mysql_fetch_array($rs);
        //print_r($row);
        return $row;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sphinx Search Test</title>
</head>

<body>
<h1>Search</h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="searchFrm"
id="searchFrm">dd
<table align="left" border="0" cellpadding="3" cellspacing="0" width="100%">
<tr>
        <td width="15%">Search Keyword</td>
        <td width="2%"></td>
        <td width="">
                <input type="text" name="keyword" id="keyword" value="" />
        </td>
</tr>
<tr><td colspan="3" height="25px"></td></tr>
<tr>
        <td colspan="3">
                <input type="submit" name="submit" value="Search" />
        </td>
</tr>
<?php if(!empty($search_result)) : ?>
<tr><td colspan="3" height="25px"></td></tr>
<tr>
        <td colspan="3" align="center">
                <!-- /The search result here -->
                <?php print $search_result; ?>
        </td>
</tr>
<?php endif; ?>
</table>
</form>
<div style="clear:both">&nbsp;</div>
<?php
//Check if the search flag is set or not
if(!empty($_POST['submit'])) {
        //$search_result = 'asdasddas';

        $start_time=microtime(true); //Starts the time counter

        $user_input = $_POST['keyword'];
        if(!empty($user_input)) {
                $client = new SphinxClient();
                $client->SetServer('localhost', 9312);
                $client->SetConnectTimeout(1);
                $client->SetArrayResult(true);
                $client->SetLimits(0,20);
                $client->SetMatchMode(SPH_MATCH_ALL);
                $client->SetSortMode(SPH_SORT_EXTENDED, "@weight asc" );
				
                $raw_result = $client->Query($user_input, "test1");
				
	
                //$raw_result = $client->Query('@Author ' . $user_input, "bt_titles");
                //echo "<hr />";
                echo "<pre>";
                print_r($raw_result['matches']);
                //echo "</pre>";
                //die;
				   $total_found = count($raw_result['matches']);
                echo "<hr />";
				echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" >";
				echo "<tr><td><strong> Search Records (".$total_found .") -Term : ".$user_input."</strong><td></tr>";
             
                $all_match = $raw_result['matches'];
                foreach($all_match as $k1 => $v1) {
                        //echo $k1;
                        //echo $v1['attrs']['t_id'];
                        $return_record = getRecords($v1['id'], $conn);
                        //print_r($return_record);
                        
						$str_prd.= "<tr><td>&nbsp;<td></tr><table cellpadding=\"3\" cellspacing=\"3\" border=\"0\" style=\"border:1px solid #ccc\" ><tr><td><strong>Product Name : </strong><br>".$return_record['products_name']."<td></tr>";
                        $all_match[$k1]['attrs']['products_name'] = $return_record['products_name'];
						$str_prd.= "<tr><td><strong>Product Desc : </strong><br>".$return_record['products_description']."<td></tr>";
                        $all_match[$k1]['attrs']['products_description'] = $return_record['products_description'];
						$str_prd.= "<tr><td><strong>Short Desc : </strong><br>".$return_record['short_desc']."<td></tr>";
                        $all_match[$k1]['attrs']['short_desc'] = $return_record['short_desc'];
						$str_prd.= "<tr><td><strong>Product URL : </strong><br>".$return_record['products_url']."<td></tr>";
                        $all_match[$k1]['attrs']['products_url'] = $return_record['products_url'];
						$str_prd.="</table>";
                }
				echo $str_prd."</table>";
                //echo "In the Author Field";
                echo "<br />";
                $stop_time=microtime(true); //Gets the end time
                echo "<br/>Time taken to post the value-sphinx-search-mysql-data-collection :
                ".number_format($stop_time-$start_time,4);
        } else {
                $search_result = 'No Keywords Given';
        }

}


?>

</body>
</html>